/**
 * CharSheetWindow.java
 * 
 * @author	Joe Scharnitzke
 * Date: 	May 31, 2011
 * 
 * This class defines the GUI for viewing and editing users'
 * Character Sheets.
 *
 * Change Log:
 * 	06/16/11 18:36 - (v1.2)
 * 						Functionality
 * 						> no longer initializes if no sheet is loaded
 * 	06/10/11 13:03 - (v1.1)
 * 						Functionality
 * 						> removed String input dialogs
 * 						> implemented Loading list
 * 	06/10/11 10:56 - (v1.0) Release
 * 	May 31, 2011 11:34:29 AM - (v0.1) Initial Implementation
 */

package gui;

import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.awt.*;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.*;

import ds.Character;
import java.util.*;
import simpio.*;

public class CharSheetWindow extends JFrame implements ActionListener
{
	private static final long serialVersionUID = 4909010228981198760L;
	
	private int i_tabs = 0;
	
	private CharLoadWindow clw_load;
	
	private JMenuBar jmb_main = new JMenuBar();
		private JMenu jm_sheet = new JMenu("Sheet");
			private JMenuItem jmi_load = new JMenuItem("Load");
			private JMenuItem jmi_close = new JMenuItem("Close");
			private JMenuItem jmi_save = new JMenuItem("Save");
		private JMenu jm_dm_tools = new JMenu("DM Tools");
			private JMenuItem jmi_combat = new JMenuItem("Track Combat");
		
	private JTabbedPane jtp_content = new JTabbedPane();
		
	private GridBagConstraints gbc_this = new GridBagConstraints();
	
	private Vector<Character> v_chars = new Vector<Character>();
	
	/**
	 * Single parameter constructor for CharSheetWindow
	 * objects.
	 * 
	 * @param char_name the name of the Character being displayed
	 */
	
	public CharSheetWindow()
	{		
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setTitle("Character Sheets");
		setLayout(new GridBagLayout());
		
		//try to load the default logo
		try
		{
			BufferedImage bi_d20 = ImageIO.read(new File("assets/images/d20icon.png"));
			setIconImage(bi_d20);
		}
		
		catch(Exception e)
		{
			System.err.println(e);
		}
		
		jmi_load.addActionListener(this);
		jmi_save.addActionListener(this);
		jmi_close.addActionListener(this);
		
		jmi_load.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O, ActionEvent.CTRL_MASK));
		jmi_save.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, ActionEvent.CTRL_MASK));
		jmi_close.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X, ActionEvent.CTRL_MASK));
		
		jmb_main.add(jm_sheet);
		jm_sheet.add(jmi_load);
		jm_sheet.add(jmi_save);
		jm_sheet.add(jmi_close);
		jmb_main.add(jm_dm_tools);
		jm_dm_tools.add(jmi_combat);
		setJMenuBar(jmb_main);
		
		try
		{
			CharParser cp_loader = new CharParser();
			clw_load = new CharLoadWindow(cp_loader.getSaved());
			String[] sa_selected = clw_load.getSelected();
			
			for(int i = 0; i < sa_selected.length; i++)
			{
				cp_loader = new CharParser(sa_selected[i]);
				Character ch_data = cp_loader.loadChar();
				CharacterPane cp_sheet = new CharacterPane(ch_data);
				v_chars.add(ch_data);
				jtp_content.add(cp_sheet);
				
				i_tabs++;
			}
		}
		
		catch(NullPointerException e)
		{
			
		}
		
		catch(Exception e)
		{
			System.err.println(e);
			return;
		}
		
		//add jtp_tabs to this
		gbc_this.fill = GridBagConstraints.BOTH;
		gbc_this.gridx = 0;
		gbc_this.gridy = 0;
		gbc_this.weightx = 1.0;
		gbc_this.weighty = 1.0;
		add(jtp_content, gbc_this);
		
		//pack();
		setSize(500,400);
		GUIHelper.Center(this);
		
		if(i_tabs > 0)
			setVisible(true);
	}

	/* (non-Javadoc)
	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
	 */
	public void actionPerformed(ActionEvent event)
	{
		if(event.getSource().equals(jmi_load))
		{
			try
			{
				boolean b_open = false;
				
				CharParser cp_loader = new CharParser();
				clw_load = new CharLoadWindow(cp_loader.getSaved());
				String[] sa_selected = clw_load.getSelected();
				
				for(int i = 0; i < sa_selected.length; i++)
				{					
					for(int j = 0; j < v_chars.size(); j++)
					{											
						if(sa_selected[i].equals(v_chars.elementAt(j).getSName()))
						{	
							b_open = true;
							break;
						}
					}
					
					if(!b_open)
					{
						cp_loader = new CharParser(sa_selected[i]);
						Character ch_data = cp_loader.loadChar();
						v_chars.add(ch_data);
						CharacterPane cp_sheet = new CharacterPane(ch_data);
						jtp_content.add(cp_sheet);
						
						i_tabs++;
					}
					

					b_open = false;
				}
				
				jtp_content.revalidate();
			}
			
			catch(Exception e)
			{
				return;
			}
		}
		
		if(event.getSource().equals(jmi_save))
		{
			CharParser cp_saver = new CharParser(jtp_content.getSelectedComponent().getName());
			cp_saver.saveChar(v_chars.elementAt(jtp_content.getSelectedIndex()));			
		}
		
		if(event.getSource().equals(jmi_close))
		{
			Component c_tab_to_close = jtp_content.getSelectedComponent();
			v_chars.remove(jtp_content.getSelectedIndex());
			jtp_content.remove(c_tab_to_close);
			
			i_tabs--;
			
			if(i_tabs < 1) dispose();
		}
	}

}
